<template>
	<view class="page">
		<view class="box" v-for="(i,index) in dataList" @tap.stop="toCus(i.id)">
			<view class="tit">
				<view class="">企业名称：</view>
				<view class="right" style="width: 76%;">{{i.name}}</view>
			</view>
			<view class="tit">
				<view class="">序 号：</view>
				<view class="right">{{i.id}}</view>
			</view>
			<view class="tit" v-if="type!=2">
				<view class="">签约时间：</view>
				<view class="right">{{i.signing_time||''}}</view>
			</view>
			<view class="line"></view>
			<view class="btn" v-if="type==1">
				<view class="btn_1" @tap.stop="toPrice(i.id)">
					<But text="查看历史报价" :bgColor="'#58B99D'" :fontSize="22" :he="50" :pad="10" :mar="6"></But>
				</view>
				<view class="btn_1" @tap.stop="toRegister(i.information,i.id)" v-if="i.information == 2">
					<But text="查看签约资料" :bgColor="'#3BA199'" :fontSize="22" :he="50" :pad="10" :mar="6"></But>
				</view>
				<view class="btn_1" @tap.stop="toRegister(i.information,i.id)" v-if="i.information == 1">
					<But text="上传签约资料" :bgColor="'#3BA199'" :fontSize="22" :he="50" :pad="10" :mar="6"></But>
				</view>
				<view class="btn_1" @tap.stop="toPdf(i.contract)">
					<But text="查看签约合同" :bgColor="'#EDC76C'" :fontSize="22" :he="50" :pad="10" :mar="6"></But>
				</view>
				<view class="btn_1" @tap.stop="toList(i.id)">
					<But text="查看节费报告" :bgColor="'#58B99D'" :fontSize="22" :he="50" :pad="10" :mar="6"></But>
				</view>
			</view>
			<view class="btn" v-if="type==2">
				<view class="btn_2" style="">
					<u-button color="#3BA199" shape="circle" size="mini" text="客户详情"
						@tap.stop="toDetail(i.id)"></u-button>
					<u-button color="#58B99D" shape="circle" size="mini" text="报价单"
						@tap.stop="toMes(i.id,1)"></u-button>
					<u-button color="#edc76c" shape="circle" size="mini" text="报价方案"
						@tap.stop="toMes(i.id,2)"></u-button>
					<u-button :color="i.status == 1?'#4579ef':'#979797'" shape="circle" size="mini" text="签约"
						@tap.stop="handleLogout(i)"></u-button>
				</view>
			</view>
		</view>
		<view style="width: 100%; text-align: center; font-size: 28rpx; margin-top: 10rpx;">{{isText}}</view>
		<u-modal :show="delShow" title="签约" content="是否签约？" close-on-click-overlay show-cancel-button @close="closeDel"
			@cancel="closeDel" @confirm="handleDel" />
	</view>
</template>

<script>
	import {
		customMy,
		signing,
		priceList,
		customDetail
	} from "@/request/homeApi.js";
	import brl from "@/request/baseUrl.js"
	import But from "@/components/Button/index.vue";
	export default {
		components: {
			But
		},
		data() {
			return {
				type: '',
				page: 1,
				last_page: 1,
				dataList: [],
				delShow: false,
				isText: '',
				pdfUrl: ''
			};
		},

		onLoad(e) {
			this.dataList = []
			this.type = e.type
			if (this.type == 1) {
				wx.setNavigationBarTitle({
					title: '我的签约客户'
				})
			} else {
				wx.setNavigationBarTitle({
					title: "我的报价"
				})
			}
			// this.getList()
		},
		onReachBottom() {
			let that = this
			if (that.page < that.last_page) {
				that.page = that.page + 1
				that.getList()
			}
		},
		onShow() {
			this.dataList = []
			this.page = 1
			this.getList()
		},
		methods: {

			// 关闭签约弹窗
			closeDel() {
				this.delShow = false;
			},
			// 打开签约弹窗
			handleLogout(item) {
				if (item.status == 1) {
					this.delShow = true;
					this.id = item.id
				}
			},
			// 报价方案
			toMes(id, type) {
				customDetail({
					id
				}).then(res => {
					if (res.code == 200) {
						let arr = res.data.quotation_detail ? res.data.quotation_detail : {}
						let t = ''

						if (type == 1) {
							t = arr.files_note
							console.log(type, t, 't1')
						} else {
							t = arr.files
							console.log(type, t, 't1')
						}

						if (t != '' && t != null) {
							this.pdfUrl = brl.baseUrl + t
							setTimeout(() => {
								uni.getSystemInfo({
									success: res => {
										let that = this
										
										const spliceLength2 = t.lastIndexOf("/");
										this.pathType = t.slice(spliceLength2 + 1);
										console.log(this.pathType)
										
										const fileName = this.pathType

										wx.downloadFile({
											url: that.pdfUrl,
											filePath: `${wx.env.USER_DATA_PATH}/${fileName}`,
											success: res => {
												const filePath = res.filePath ||
													res.tempFilePath
												console.log(filePath)
												wx.openDocument({
													filePath: filePath,
													showMenu: true, // 显示右上角菜单
													fileType: 'pdf',
													success: () => {
														console.log(
															'打开文档成功'
															)
													},
													fail: err => {
														console.log(
															`打开文档失败`,
															err)
													}
												})
											},
											fail: err => {
												console.log(`下载文档失败`, err)
											}
										})

									}
								});
							}, 500)
						} else {
							uni.showToast({
								title: '暂无',
								duration: 1500,
								icon: 'none',
							})
						}
					} else {
						uni.showToast({
							title: res.msg,
							duration: 1500,
							icon: 'none',
						})
					}
				})
			},
			// 签约
			handleDel() {
				signing({
					custom_lists_id: this.id
				}).then(res => {
					// uni.showModal({
					// 	title: '',
					// 	content: res.msg,
					// });
					if (res.code == 200) {
						this.delShow = false;
						uni.navigateTo({
							url: `/pages/bind/register?id=${this.id}`
						})
						this.dataList.forEach(item => {
							if (item.id == this.id) {
								item.status = 2
							}
						})
					}
				})
			},
			// 上传资料
			toRegister(type, id) {
				uni.navigateTo({
					url: `/pages/bind/register?type=${type}&&id=${id}`
				})
			},
			toCus(id) {
				console.log(this.type, 'type')
				if (this.type == 1) {
					uni.navigateTo({
						url: `/pages/home/company?id=${id}&&type=${this.type}`
					})
				} else {
					uni.navigateTo({
						url: `/pages/home/bookList?id=${id}&&type=${this.type}`
					})
				}
			},
			toDetail(id) {
				uni.navigateTo({
					url: `/pages/home/company?id=${id}`
				})

			},
			// 查看报价
			toPrice(id) {
				if (this.type == 2) {
					uni.navigateTo({
						url: `/pages/home/bookList?id=${id}`
					})
				} else {
					uni.navigateTo({
						url: `/pages/home/bookList?id=${id}`
					})
				}

			},
			// 节费报告
			toList(id) {
				uni.navigateTo({
					url: `/pages/my/remarkMessage?id=${id}`
				})
			},
			// 签约合同
			toPdf(files) {
				if (files != null && files != '') {
					this.pdfUrl = brl.baseUrl + files
					setTimeout(() => {
						uni.getSystemInfo({
							success: res => {
								console.log(this.pdfUrl, 'pdfUrl');
								wx.downloadFile({
									url: this.pdfUrl,
									// filePath: wx.env.USER_DATA_PATH,
									success: function(res) {
										console.log(res, 'tempFilePath');
										wx.openDocument({
											showMenu: true,
											filePath: res.tempFilePath,
											success: function(res) {
												console.log('打开文档成功');
											}
										});
									}
								});
							}
						});
					}, 1000)
				} else {
					uni.showToast({
						title: '暂无',
						duration: 1500,
						icon: 'none',
					})
				}
			},
			// 客户列表
			getList() {
				let that = this
				let id = uni.getStorageSync('userInfo') ? JSON.parse(uni.getStorageSync('userInfo')).user_id : ''
				if (this.type == 1) {
					customMy({
						page: that.page,
						name: '',
						address: '',
						entry_time: '',
						limit: 10,
						custom_industry_id: '',

					}).then(res => {
						res.data.count == 0 ? that.isText = '暂无' : that.isText = ''
						that.dataList = that.dataList.concat(res.data.list)
						console.log(that.dataList, 'that.dataList ,,,')
						that.last_page = Math.ceil(res.data.count / res.data.list.length)
					})
				} else {
					let id = uni.getStorageSync('userInfo') ? JSON.parse(uni.getStorageSync('userInfo')).user_id : ''
					priceList({
						page: that.page,
						name: '',
						entry_time: '',
						limit: 10,
						user_id: id
					}).then(res => {
						res.data.count == 0 ? that.isText = '暂无' : that.isText = ''
						that.dataList = that.dataList.concat(res.data.list)
						that.last_page = Math.ceil(res.data.count / res.data.list.length)
					})
				}

			},
		},
	};
</script>

<style lang="scss" scoped>
	button::after {
		border: none;
		background-color: transparent !important;
		padding: 0;
	}

	.page {
		.box {
			margin: 20rpx 28rpx;
			border-radius: 20rpx;
			background-color: #F7F7F7;
			padding: 10rpx;

			.tit {
				display: flex;
				justify-content: flex-start;
				line-height: 46rpx;
				font-size: 28rpx;
				color: #979797;
				margin: 10rpx;

				.right {
					color: #000;
				}
			}

			.line {
				background: #D9D9D999;
				height: 2rpx;
				margin-bottom: 20rpx;
			}

			.btn {
				text-align: right;
				height: 60rpx;
				width: 100%;
				line-height: 60rpx;
				display: flex;
				justify-content: flex-end;

				.btn_1 {
					width: 26%;
					margin: 0 8rpx;
				}

				.btn_2 {
					width: 100%;
					margin: 0 8rpx;
					display: flex;
					justify-content: flex-end;

					/deep/ .u-button {
						margin-left: 20rpx;
					}
				}
			}
		}
	}
</style>